{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T15:20:39.581062Z",
     "start_time": "2025-01-07T15:20:39.576492Z"
    }
   },
   "cell_type": "code",
   "source": "import numpy as np",
   "id": "5b8e11ad2ced0317",
   "outputs": [],
   "execution_count": 63
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "##### numpy.ndarray 的特点\n",
    "##### 多维数组：可以表示一维、二维、三维甚至更高维的数组。\n",
    "##### 同质性：数组中的所有元素必须是相同的数据类型（如 int、float 等）。\n",
    "##### 高效性：基于 C 语言实现，支持向量化操作，性能远高于 Python 原生列表。\n",
    "##### 丰富的操作：支持数学运算、逻辑运算、切片、索引、广播等操作。"
   ],
   "id": "bf2d35558c389062"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 创建一维数组",
   "id": "35f5b9dc5fd493db"
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-01-07T15:20:39.735977Z",
     "start_time": "2025-01-07T15:20:39.730422Z"
    }
   },
   "source": [
    "list1=[1,2,3,4]\n",
    "oneArray=np.array(list1)\n",
    "print(type(oneArray))\n",
    "print(oneArray)\n",
    "# 创建数组的多种形式\n",
    "# 1.直接传入列表的方式\n",
    "t1=np.array([1,2,3])\n",
    "print(t1)\n",
    "print(type(t1))\n",
    "# 2.传入range生成序列\n",
    "t2=np.array(range(10))\n",
    "print(t2)\n",
    "print(type(t2))\n",
    "# 3.使用numpy自带的np.arange()生成数组\n",
    "t3=np.arange(0,10,2)\n",
    "print(t3)\n",
    "print(type(t3))"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n",
      "[1 2 3 4]\n",
      "[1 2 3]\n",
      "<class 'numpy.ndarray'>\n",
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "<class 'numpy.ndarray'>\n",
      "[0 2 4 6 8]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 64
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 创建二维数组",
   "id": "13736cf4a6255775"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T15:20:39.741875Z",
     "start_time": "2025-01-07T15:20:39.737974Z"
    }
   },
   "cell_type": "code",
   "source": [
    "list2=[[1,2],[3,4],[5,6]]\n",
    "twoArray=np.array(list2)\n",
    "print(twoArray)"
   ],
   "id": "ec0c14e54ee5b4c0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n"
     ]
    }
   ],
   "execution_count": 65
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 常用属性",
   "id": "852c493c8fa4b"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T15:20:39.756793Z",
     "start_time": "2025-01-07T15:20:39.752849Z"
    }
   },
   "cell_type": "code",
   "source": [
    "list2=[[1,2],[3,4],[5,6]]\n",
    "twoArray=np.array(list2)\n",
    "#获取数组的维度(注意：与函数的参数很像)\n",
    "print(twoArray.ndim)\n",
    "#形状（行，列）\n",
    "print(twoArray.shape)\n",
    "#有多少个元素\n",
    "print(twoArray.size)"
   ],
   "id": "4c90929f95c09ff0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "(3, 2)\n",
      "6\n"
     ]
    }
   ],
   "execution_count": 66
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 数组与list转化",
   "id": "52c012a8a11712a8"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T15:20:39.772485Z",
     "start_time": "2025-01-07T15:20:39.768763Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#将数组转成list\n",
    "a=np.array([9,12,88,14,25])\n",
    "list_a=a.tolist()\n",
    "print(list_a)\n",
    "print(type(list_a))"
   ],
   "id": "b4cdf2c484a78445",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[9, 12, 88, 14, 25]\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "execution_count": 67
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 调整数组的形状",
   "id": "f02ab789ea11729e"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T15:20:39.830087Z",
     "start_time": "2025-01-07T15:20:39.824549Z"
    }
   },
   "cell_type": "code",
   "source": [
    "four=np.array([[1,2,3],[4,5,6]])\n",
    "#修改的是原有的\n",
    "four.shape=(3,2)\n",
    "print(four)\n",
    "#返回一个新的数组\n",
    "four=four.reshape(3,2)\n",
    "print(four)\n",
    "#将多维变成一维数组\n",
    "five=four.reshape((6,),order='F')\n",
    "#默认情况下‘C’以行为主的顺序展开，‘F’（Fortran风格）意味着以列的顺序展开\n",
    "six=four.flatten(order='F')\n",
    "print(five)\n",
    "print(six)\n",
    "#拓展：数组的形状\n",
    "t=np.arange(24)\n",
    "print(t)\n",
    "print(t.shape)\n",
    "#转换成二维\n",
    "t1=t.reshape((4,6))\n",
    "print(t1)\n",
    "print(t1.shape)"
   ],
   "id": "a46a23a8b466c445",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "[1 3 5 2 4 6]\n",
      "[1 3 5 2 4 6]\n",
      "[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]\n",
      "(24,)\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "(4, 6)\n"
     ]
    }
   ],
   "execution_count": 68
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### NumPy的数据类型",
   "id": "5df517d40fc71d8e"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T15:20:39.867777Z",
     "start_time": "2025-01-07T15:20:39.862003Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import random\n",
    "#返回数组中每个元素的字节单位长度，dtype设置数据类型\n",
    "f=np.array([1,2,3,4,5],dtype=np.int16)\n",
    "print(f.itemsize)#1np.int8(一个字节)\n",
    "#获取数据类型\n",
    "print(f.dtype)\n",
    "#调整数据类型\n",
    "f1=f.astype(np.int64)\n",
    "print(f1.dtype)\n",
    "#拓展随机生成小数\n",
    "#使用python语法，保留两位\n",
    "print(round(random.random(),2))\n",
    "arr=np.array([random.random()for i in range(10)])\n",
    "#取小数点后两位\n",
    "print(np.round(arr,2))"
   ],
   "id": "d72b16fc4dcc780",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "int16\n",
      "int64\n",
      "0.03\n",
      "[0.79 0.   0.79 0.07 0.3  0.2  0.49 0.49 0.5  0.39]\n"
     ]
    }
   ],
   "execution_count": 69
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 数组的计算",
   "id": "3ef41f88e5721ffd"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "##### 数组和数的运算：由于numpy的广播机机制在运算过程中，加减乘除的值被广播到所有的元素上面。",
   "id": "822578d66edbfaf1"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T15:20:39.912856Z",
     "start_time": "2025-01-07T15:20:39.908671Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t1=np.arange(24).reshape((6,4))\n",
    "print(t1+2)\n",
    "print(\"-\"*20)\n",
    "print(t1*2)\n",
    "print(\"-\"*20)\n",
    "print(t1/2)\n",
    "# 学过线代，不难理解"
   ],
   "id": "957fdf5230550181",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 2  3  4  5]\n",
      " [ 6  7  8  9]\n",
      " [10 11 12 13]\n",
      " [14 15 16 17]\n",
      " [18 19 20 21]\n",
      " [22 23 24 25]]\n",
      "--------------------\n",
      "[[ 0  2  4  6]\n",
      " [ 8 10 12 14]\n",
      " [16 18 20 22]\n",
      " [24 26 28 30]\n",
      " [32 34 36 38]\n",
      " [40 42 44 46]]\n",
      "--------------------\n",
      "[[ 0.   0.5  1.   1.5]\n",
      " [ 2.   2.5  3.   3.5]\n",
      " [ 4.   4.5  5.   5.5]\n",
      " [ 6.   6.5  7.   7.5]\n",
      " [ 8.   8.5  9.   9.5]\n",
      " [10.  10.5 11.  11.5]]\n"
     ]
    }
   ],
   "execution_count": 70
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "##### 数组和数组之间的操作：同种形状的数组(对应位置进行计算操作)，不种形状的多维数组不能计算",
   "id": "a7ad190d2e759dd"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T15:20:39.943898Z",
     "start_time": "2025-01-07T15:20:39.939788Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t1=np.arange(24).reshape((6,4))\n",
    "t2=np.arange(100,124).reshape((6,4))\n",
    "print(t1+t2)\n",
    "print(t1*t2)\n",
    "# 同样类似于线代中矩阵的加法，但和矩阵乘法不一样"
   ],
   "id": "277544d233b1ec21",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[100 102 104 106]\n",
      " [108 110 112 114]\n",
      " [116 118 120 122]\n",
      " [124 126 128 130]\n",
      " [132 134 136 138]\n",
      " [140 142 144 146]]\n",
      "[[   0  101  204  309]\n",
      " [ 416  525  636  749]\n",
      " [ 864  981 1100 1221]\n",
      " [1344 1469 1596 1725]\n",
      " [1856 1989 2124 2261]\n",
      " [2400 2541 2684 2829]]\n"
     ]
    }
   ],
   "execution_count": 71
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "##### 数组和数组之间的操作：行数或者列数相同的一维数组和多维数组可以进行计算",
   "id": "27aff085f5e4e1a"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T15:20:39.981578Z",
     "start_time": "2025-01-07T15:20:39.977813Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#行形状相同（会与每一行数组的对应位相操作)\n",
    "t1 =np.arange(24).reshape((4,6))\n",
    "t2 = np.arange(0,6)\n",
    "print(t1-t2)\n",
    "# 每一行都进行该操作，不难理解"
   ],
   "id": "8e9f7484de69f72",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  0  0  0  0  0]\n",
      " [ 6  6  6  6  6  6]\n",
      " [12 12 12 12 12 12]\n",
      " [18 18 18 18 18 18]]\n"
     ]
    }
   ],
   "execution_count": 72
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T15:20:39.998326Z",
     "start_time": "2025-01-07T15:20:39.993565Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#列形状相同（会与每一个相同维度的数组的对应位相操作)\n",
    "t1 =np.arange(24).reshape((4,6))\n",
    "t2 = np.arange(4).reshape((4,1))\n",
    "print(t1-t2)\n",
    "# 行操作变成列操作，这也不难理解"
   ],
   "id": "c52c26aceba3cf6b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 5  6  7  8  9 10]\n",
      " [10 11 12 13 14 15]\n",
      " [15 16 17 18 19 20]]\n"
     ]
    }
   ],
   "execution_count": 73
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 数组的索引和切片",
   "id": "2ac610187fb52234"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "##### 一维数组的操作：",
   "id": "e83fb73a3704a392"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T15:20:40.027789Z",
     "start_time": "2025-01-07T15:20:40.023262Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.arange(10)\n",
    "print(a)\n",
    "print(a[2])\n",
    "print(a[2:])\n",
    "# 类似于列表，简单"
   ],
   "id": "2e8a7fc19596e60d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "2\n",
      "[2 3 4 5 6 7 8 9]\n"
     ]
    }
   ],
   "execution_count": 74
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "##### 多维数组的操作：",
   "id": "520552af143dc903"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T15:20:40.082957Z",
     "start_time": "2025-01-07T15:20:40.078658Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 初始化多维数组\n",
    "t1 = np.arange(24).reshape(4,6)\n",
    "print(t1)"
   ],
   "id": "9f39f0231c74ab42",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "execution_count": 75
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T15:20:40.103539Z",
     "start_time": "2025-01-07T15:20:40.099915Z"
    }
   },
   "cell_type": "code",
   "source": "print(t1[1]) # 取一行(一行代表是一条数据，索引也是从0开始的)print(t1[1,:])",
   "id": "d3f927d77adea229",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 6  7  8  9 10 11]\n"
     ]
    }
   ],
   "execution_count": 76
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T15:20:40.141948Z",
     "start_time": "2025-01-07T15:20:40.138449Z"
    }
   },
   "cell_type": "code",
   "source": "print(t1[1:])# 取连续的多行",
   "id": "85ac3c91a18acf40",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "execution_count": 77
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T15:20:40.168543Z",
     "start_time": "2025-01-07T15:20:40.164890Z"
    }
   },
   "cell_type": "code",
   "source": "print(t1[1:3,:])# 取连续的多行",
   "id": "5985a5f7e3cc3579",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]]\n"
     ]
    }
   ],
   "execution_count": 78
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T15:20:40.178690Z",
     "start_time": "2025-01-07T15:20:40.175527Z"
    }
   },
   "cell_type": "code",
   "source": "print(t1[[0,2,3]])# 取不连续的多行",
   "id": "6380e69f56741509",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "execution_count": 79
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T15:20:40.206567Z",
     "start_time": "2025-01-07T15:20:40.202630Z"
    }
   },
   "cell_type": "code",
   "source": "print(t1[[0,2,3],:])# 取不连续的多行",
   "id": "db29af04165df789",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "execution_count": 80
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T15:20:40.239205Z",
     "start_time": "2025-01-07T15:20:40.235494Z"
    }
   },
   "cell_type": "code",
   "source": "print(t1[:,1])# 取一列",
   "id": "e729ca21ff8d97aa",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1  7 13 19]\n"
     ]
    }
   ],
   "execution_count": 81
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T15:20:40.270861Z",
     "start_time": "2025-01-07T15:20:40.267134Z"
    }
   },
   "cell_type": "code",
   "source": "print(t1[:,1:])# 连续的多列",
   "id": "9fa3cc53b475fd48",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3  4  5]\n",
      " [ 7  8  9 10 11]\n",
      " [13 14 15 16 17]\n",
      " [19 20 21 22 23]]\n"
     ]
    }
   ],
   "execution_count": 82
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T15:20:40.288702Z",
     "start_time": "2025-01-07T15:20:40.284830Z"
    }
   },
   "cell_type": "code",
   "source": "print(t1[:,[0,2,3]])# 取不连续的多列",
   "id": "441cc61149528504",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  2  3]\n",
      " [ 6  8  9]\n",
      " [12 14 15]\n",
      " [18 20 21]]\n"
     ]
    }
   ],
   "execution_count": 83
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T15:20:40.313707Z",
     "start_time": "2025-01-07T15:20:40.309649Z"
    }
   },
   "cell_type": "code",
   "source": "print(t1[2,3])# # 取某一个值,三行四列",
   "id": "a5c8e7ee12bdb612",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15\n"
     ]
    }
   ],
   "execution_count": 84
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 数组中的数值修改",
   "id": "eb707cab5934cee3"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T15:20:40.346392Z",
     "start_time": "2025-01-07T15:20:40.342632Z"
    }
   },
   "cell_type": "code",
   "source": "t = np.arange( 24 ).reshape( 4, 6 )",
   "id": "2b92dda47ebad0f3",
   "outputs": [],
   "execution_count": 85
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T15:20:40.387890Z",
     "start_time": "2025-01-07T15:20:40.384293Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# # 修改某一行的值\n",
    "t[1, :] = 0"
   ],
   "id": "9b31f213c3de66cb",
   "outputs": [],
   "execution_count": 86
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T15:20:40.423787Z",
     "start_time": "2025-01-07T15:20:40.420314Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 修改某一列的值\n",
    "t[:, 1] = 0"
   ],
   "id": "272d9cf317bdd8d7",
   "outputs": [],
   "execution_count": 87
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T15:20:40.445962Z",
     "start_time": "2025-01-07T15:20:40.442737Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 修改连续多行\n",
    "t[1:3, :] = 0"
   ],
   "id": "36ee9872662889ce",
   "outputs": [],
   "execution_count": 88
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T15:20:40.469893Z",
     "start_time": "2025-01-07T15:20:40.466687Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 修改连续多列\n",
    "t[:, 1:4] = 0"
   ],
   "id": "82d3785d1c41a55b",
   "outputs": [],
   "execution_count": 89
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T15:20:40.504854Z",
     "start_time": "2025-01-07T15:20:40.501811Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 修改多行多列，取第二行到第四行，第三列到第五列\n",
    "t[1:4,2:5]=0"
   ],
   "id": "b3f4088cd6fb51fd",
   "outputs": [],
   "execution_count": 90
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T15:20:40.513170Z",
     "start_time": "2025-01-07T15:20:40.509842Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 修改多个不相邻的点\n",
    "t[[0,1],[0,3]]=0"
   ],
   "id": "5fb6caafad728b7b",
   "outputs": [],
   "execution_count": 91
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T15:20:40.552807Z",
     "start_time": "2025-01-07T15:20:40.549079Z"
    }
   },
   "cell_type": "code",
   "source": [
    " # 可以根据条件修改，比如讲小于10的值改掉\n",
    "t[t < 10] = 0"
   ],
   "id": "fbe0b202ef95bafc",
   "outputs": [],
   "execution_count": 92
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T15:20:40.561120Z",
     "start_time": "2025-01-07T15:20:40.556808Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 使用逻辑判断\n",
    "# np.logical_and & # np.logical_or |\n",
    "# np.logical_not ~\n",
    "t[(t > 2) & (t < 6)] = 0 # 与\n",
    "t[(t < 2) | (t > 6)] = 0 # 或\n",
    "t[~(t > 6)] = 0 # 非"
   ],
   "id": "5f545c11e4e9d748",
   "outputs": [],
   "execution_count": 93
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T15:20:40.577264Z",
     "start_time": "2025-01-07T15:20:40.573094Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t=t.clip(10,18)\n",
    "print(t)"
   ],
   "id": "28ba48316d8efb85",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[10 10 10 10 10 10]\n",
      " [10 10 10 10 10 10]\n",
      " [10 10 10 10 10 10]\n",
      " [10 10 10 10 10 10]]\n"
     ]
    }
   ],
   "execution_count": 94
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# # 拓 展\n",
    "# # 三目运算 np.where(condition, x, y)满足条件(condition)，输出x，不满足输出y。)\n",
    "score = np.array( [[80, 88], [82, 81], [75, 81]] )\n",
    "result = np.where( score > 80, True, False )\n",
    "print( result )"
   ],
   "id": "49210b81a2f9a139"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
